<template>
	<view class="page-wraper">
		<customNav style="padding-top: 20px;" title1="身份认证" titleColor="#000026" bgColor="white" :backIcon="true" backIconColor="black"></customNav>
		<view class="bkg" >
			<view  v-if="examine < 3" class="review_status">
				{{reviewStatus}}
			</view>
			<view style="height: 10px;"></view>
			<view class="" style="background-color: white; height:auto;">
				<view class="cell">
					<view class="cell_content">
						<view class="cell_title" style="flex:1;">
							* 真实姓名
						</view>
						<view class="cell_right" style="">
							<input :disabled="pageType == 1" v-model="realName" type="text" placeholder="请输入" placeholder-class="placeholder_text" style="text-align: right;">
						</view>
					</view>
					<view class="cell_line"></view>
				</view>
				
				<view class="cell">
					<view class="cell_content">
						<view class="cell_title" style="flex:1;">
							* 身份证号码
						</view>
						<view class="cell_right" style="">
							<input :disabled="pageType == 1" v-model="cardId" type="idcard" placeholder="请输入" placeholder-class="placeholder_text" style="text-align: right;">
						</view>
					</view>
					<view class="cell_line"></view>
				</view>
			</view>
			
			<view v-if="examine > 0" class="enter_btn center" @click="enter(pageType)">
				{{enterText}}
			</view>
		
		</view>
	</view>
</template>

<script>
	import customNav from "@/components/custom-nav/custom-nav.vue";
	import cssArcBackground from "@/components/cssArcBackground/cssArcBackground.vue";
	import addressSearch from "@/components/addressSearch/addressSearch.vue";
	import {addPerson, getPersonInfo} from '@/api/user.js'
	
	export default {
		components: {
			customNav,
			addressSearch,
			cssArcBackground
		},
		data() {
			return {
				userId: '',
				realName: '',
				cardId: '',
				personData: {},
				pageType: 0,	//页面类型 0：新增，1：查看，2: 编辑
				enterText:"",		
				reviewStatus: "",
				examine: 0,
			}
		},
		onLoad() {
			
			uni.getStorage({
				key: 'login',
				success: (res) => {
					let userInfo = res.data
					this.userId = userInfo.id
					this.getPersonAuthInfo()
				}
			})
		},
		methods: {
			
			getPersonAuthInfo() {
				getPersonInfo(this.userId)
				.then(res => {
					let data = res.data
					if (data.length == 0) {
						this.pageType = 0;	
						this.examine = 3 	//新
					} else {
						this.pageType = 1;	//查看
						this.realName = data.encryption_full_name
						this.cardId = data.encryption_identity
						this.personData = data							
						let examine = data.examine
						this.examine = examine
						if (examine == 0) {
							this.reviewStatus = '身份认证审核中'
						} else if (examine == 1) {
							this.reviewStatus = '您已通过身份认证'
						} else if (examine == 2) {
							this.reviewStatus = '您的身份认证失败'
						}	
					}
					this.enterChange()
				})
				.catch(e => {
					console.log(e)
				})
			},
			
			addLicensPhoto() {
				uni.chooseImage({
					count: 1, 
					sourceType: ['album'],
					success:function( res ){
						console.log("========" + JSON.stringify(res.tempFilePaths))
					}
				})
			},
			
			enterChange() {
				let type = this.pageType
				if (type == 0) {
					this.enterText = "确认提交";
				} else if (type == 1) {
					this.enterText = "再次编辑";
				} else if (type == 2) {
					this.enterText = "再次提交审核"
				}
				
				if (type == 1) {
					this.realName = this.personData.encryption_full_name
					this.cardId = this.personData.encryption_identity
				} else {
					this.realName = this.personData.full_name
					this.cardId = this.personData.identity
				}
			},
			
			
			enter() {
				let pageType = this.pageType
				
				if (pageType == 1) {
					this.pageType = 2;
					this.enterChange()
					return
				} 
				
				let is_update = "0"
				if (pageType == 2) {
					is_update = "1"
				}
				
				let param = {
					"users_id" : this.userId,
					"full_name" : this.realName,
					"identity" : this.cardId,
					"is_update" : is_update
				}
				addPerson(param)
				.then(res => {
					uni.showToast({
						title: res.message,
						icon: 'none',
						duration: 3000,
					})
					
					if (res.code == 0) {
						
						clearTimeout(this.timer);  //清除延迟执行
						this.timer = setTimeout(()=>{   //设置延迟执行
						
							uni.navigateBack()
						
						}, 3000);
					}
				})
			}
		}
	}
</script>

<style>
	
	.review_status {
		width: 100%;
		height: 40px;
		background: #EDF1FF;
		opacity: 1;
		font-size: 16px;
		font-family: Source Han Sans CN;
		font-weight: 500;
		color: #0E59EA;
		display: flex;
		align-items: center;
		padding-left: 14px;
	}
	
	.enter_btn {
		position: absolute;
		height: 47px;
		background: #0E59EA;
		opacity: 1;
		border-radius: 7px;
		left: 24px;
		right: 24px;
		bottom: 50px;
		
		font-size: 16px;
		font-family: Source Han Sans CN;
		font-weight: 500;
		color: #FFFFFF;
	}
	
	.license_img {
		margin-left: 30px;
		/* margin-top: 17px; */
		width: 113px;
		height: 91px;
		background: #F4F4F4;
		opacity: 1;
		border-radius: 7px;
	}
	
	.cell_title {
		font-size: 14px;
		font-family: PingFang SC;
		font-weight: 500;
		color: #333333;
	}
	
	.cell_right {

	}
	
	.cell_line {
		margin-left: 15px;
		margin-right: 15px;
		border: 1px solid #000000;
		opacity: 0.04;
	}
	
	.placeholder_text {
		font-size: 14px;
		font-family: PingFang SC;
		font-weight: 500;
		color: #999999;
	}
	
	.cell {
		display: flex;
		flex-direction: column;
		/* background-color: red; */
	}
	
	.cell_content {
		display: flex;
		flex-direction: row;
		height: 60px;
		align-items: center;
		margin-left: 14px;
		margin-right: 14px;
		/* background-color: red; */
	}
	
	.bkg {
		position: absolute;
		width: 100%;
		height: 90%;
		background-color: #F2F2F2;
	}
	
	.center {
		display: flex;
		justify-content: center;
		align-items: center;
	}
	
	
</style>